/*
 * Copyright (c) 2018-2999 广州亚米信息科技有限公司 All rights reserved.
 *
 * https://www.gz-yami.com/
 *
 * 未经允许，不可做商业用途！
 *
 * 版权所有，侵权必究！
 */

package com.yami.shop.admin.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.yami.shop.bean.param.CouponImportParam;
import com.yami.shop.bean.model.CouponRecord;
import com.yami.shop.service.CouponRecordService;
import com.yami.shop.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * @author lgh on 2018/10/16.
 */
@Api(tags = "优惠券管理")
@RestController
@RequestMapping("/admin/coupon")
public class CouponRecordController {

    @Autowired
    private CouponRecordService couponRecordService;
    @Autowired
    private UserService userService;

    /**
     * 导入优惠券记录
     * @param couponImportParams
     * @return
     */
    @PostMapping("/import")
    @ApiOperation(value = "导入优惠券记录", notes = "导入优惠券记录")
    public ResponseEntity importScore(@RequestBody CouponImportParam[] couponImportParams){
        for (CouponImportParam c : couponImportParams) {
            userService.addCoupon(c.getUserCode(), c.getCount(),"优惠券导入");
        }
        return ResponseEntity.ok().build();
    }


    @PostMapping("/send")
    @ApiOperation(value = "发放优惠券")
    public ResponseEntity send(@ApiParam(value = "用户id", required = true) String uid,
                               @ApiParam(value = "优惠券数量", required = true) Integer couponCount) {
        userService.addCouponByUid(uid,couponCount,"后台发放");
        return ResponseEntity.ok().build();
    }


    @GetMapping("/history")
    @ApiOperation(value = "查看某人的优惠券记录")
    public ResponseEntity get(@ApiParam(value = "用户id", required = true) String uid,
                              @ApiParam(value = "类型 1发放 2使用", required = true) Integer type) {
        LambdaQueryWrapper<CouponRecord> query = Wrappers.lambdaQuery(new CouponRecord())
                .eq(CouponRecord::getUid, uid)
                .eq(type!=null,CouponRecord::getType,type);
        List<CouponRecord> list = couponRecordService.list(query);

        return ResponseEntity.ok(list);
    }


}
